package com.springboot.astra.controller;

import com.springboot.astra.bean.Role;
import com.springboot.astra.bean.UserRoles;
import com.springboot.astra.result.DataResult;
import com.springboot.astra.service.*;
import com.springboot.astra.util.DateUtils;
import com.springboot.astra.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/role")
public class RolesController {
    @Autowired
    RoleService roleService;
    @Autowired
    JwtUtils jwtUtils;
    @Autowired
    UserRolesService userRolesService;
    @Autowired
    UserService userService;
    @Autowired
    RoleResourceService roleResourceService;
    @Autowired
    RolesPermissionService rolesPermissionService;

    /**
     * 查询一个角色的信息
     * @param map
     * @return
     */
    @PostMapping("/getRole")
    public DataResult getRole(@RequestParam Map<String,String> map) {
        Integer id = Integer.valueOf(map.get("roleId"));
        Role role = roleService.getOneById(id);
        return  DataResult.success(role);
    }

    /**
     * 获取所有角色
     * @return
     */
    @GetMapping("/getAllRole")
    public DataResult getAllRole(){
        List<Role> roles = roleService.getAll();
        return DataResult.success(roles);
    }
    @PutMapping("/insterRoleForUser")
    @Transactional
    public DataResult insterRoleForUser(@RequestBody UserRoles userRoles) throws Exception{
        if (userRolesService.isExact(userRoles) == null){
            userRolesService.insertUserRoles(userRoles);
            return DataResult.success();
        }else {
            return DataResult.failWithMsg("该用户已经拥有此权限");
        }
    }

    /**
     * 查询所有角色
     * @return
     */
    @GetMapping("/getAll")
    public DataResult getAll() {
        List<Role> roles = getRolePermissionResource();
        return  DataResult.success(roles);
    }

    /**
     * 添加角色
     * @param role
     * @return
     */
    @PutMapping("/addRole")
    public DataResult addRole(@RequestBody Role role){
        role.setCreateTime(DateUtils.getTime());
        role.setUpdateTime(DateUtils.getTime());
        roleService.insertRole(role);
        return DataResult.success(200,"success","添加角色成功");
    }
    @PutMapping("/updateRole")
    public DataResult updateRole(@RequestBody Role role) {
        role.setUpdateTime(DateUtils.getTime());
        roleService.updateOneById(role);
        return DataResult.success(200,"success","更新角色成功");
    }
    @PostMapping("/deleteRole")
    public DataResult deleteRole(@RequestParam Map<String,String> map) throws Exception {
        Integer id = Integer.valueOf(map.get("roleId"));
        //禁止删除基本角色
        if (id == 1 || id == 2) {
            return DataResult.failWithMsg("无法删除基本角色");
        }
        //删除该角色表中的数据
        roleService.deleteOneById(id);
        //并且删除该角色的资源-角色
        roleResourceService.deleteAll(id);
        //删除该角色的角色-权限
        rolesPermissionService.deleteAll(id);
        //删除该角色的角色-用户
        userRolesService.deleteOneById(id);
        return DataResult.success(200,"success","删除角色成功");
    }
    /**
     * 获取所有角色并且组装该角色的资源+权限
     * @param
     * @return
     */
    private List<Role> getRolePermissionResource(){
        List<Role> rolePermissionResource = new LinkedList<Role>();
        List<Role> roles = roleService.getOnlyRoles();
        //遍历这个角色
        for (int i=0; i<roles.size(); i++){
            //获得一个角色
            Role role = roles.get(i);
            //获得这个角色所属的权限
            //挂载角色权限
            role.setPermissions(roleService.getPermission(role.getRoleId()));
            role.setResources(roleService.getResource(role.getRoleId()));
            rolePermissionResource.add(role);
        }
        return rolePermissionResource;
    }
}
